package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Slf4j
@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageDTO pagelist(Integer page, Integer size) {

        PageDTO<Item> pageDTO = new PageDTO<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("page", (page - 1) * size);
        hashMap.put("size", size);
        ArrayList<Item> items = itemMapper.page(hashMap);
        Long count = itemMapper.getcount();
        pageDTO.setList(items);
        pageDTO.setTotal(count);
        return pageDTO;
    }

    @Override
    public Item findById(Long id) {

        Item item = itemMapper.findById(id);

        if (item == null) {
            log.info("查询为空");
            return item;
        }
        return item;
    }
}
